<?php

include_file('src/02_class/AJobTool.abstract_class.php');

/**
 * DownloadFileJobTool - Allow easy downloading of file
 * 
 * @uses php-curl
 * @author polux       
 */
class DownloadFileJobTool extends AJobTool {	
	// ***********************  Constructor  *********************** //
	/**
	 * Default Constructor
	 * 
	 * @param AJob 		$p_o_jobObj			Job Object reference
	 */
	function __construct(&$p_o_jobObj) {
		parent::__construct($p_o_jobObj);		
	}
	
	// *********************** Public Methods  *********************** //
	/**
	 * defineMandatoryProperties - Set default properties
	 * 
	 * @see AJobTool::defineMandatoryProperties()
	 */
	function defineMandatoryProperties()
	{
		$this->_as_jobToolPropertiesDef[] = "INPUT_URL";
		$this->_as_jobToolPropertiesDef[] = "OUPUT_FILEPATH";
		$this->_as_jobToolPropertiesDef[] = "POST_DATA";
	}
	/**
	 * RunJobTool - Download file and save it
	 * 
	 * @param 	$p_as_options	Options
	 * @see AJobTool::RunJobTool()
	 */
	function RunJobTool($p_as_options)
	{
		// Loading properties
		$this->loadProperties($p_as_options,true,true);		
		
		$l_s_url = $this->getPropertyValue("INPUT_URL");
		$l_s_outputfilepath = $this->getPropertyValue("OUPUT_FILEPATH");
		$l_as_data = $this->getPropertyValue("POST_DATA");
		
		
		// Check Pre-requesites
		if(!$this->cURLcheckBasicFunctions())
			throw new JobToolGenericException("Curl basics function pre-requesites can't be satisfied. Download aborted.");

		// Curl initialization
		$l_o_ch = curl_init();
		
		// Download main processing
		if($l_o_ch)
		{
			// Destination file opening
			$l_o_fileOut = fopen($l_s_outputfilepath, "w");
			
			// File OK
			if($l_o_fileOut)
			{
				// CURL Options checks
				if( !curl_setopt($l_o_ch, CURLOPT_URL, $l_s_url) )
				{
					fclose($l_o_fileOut); // to match fopen()
					curl_close($l_o_ch); // to match curl_init()
					throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_URL)");	
				}
				
// 				$redirects = 0;
// 				// Define internal parameters
// 				if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) 
// 				{
					curl_setopt($l_o_ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
					
				//	curl_setopt($l_o_ch, CURLOPT_RETURNTRANSFER, 0);
					
					
 					if( !curl_setopt($l_o_ch, CURLOPT_HEADER,  true)) 
 						throw new JobGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_HEADER)");
// 					if( !curl_setopt($l_o_ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) 
// 						throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)");
 					
 					
 					//if(!array_key_exists("URI", $l_as_data))
 					if( !curl_setopt($l_o_ch, CURLOPT_FILE, $l_o_fileOut) ) 
 						throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_FILE)");
// 					if( !curl_setopt($l_o_ch, CURLOPT_MAXREDIRS, $redirects) ) 
// 						throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_MAXREDIRS)");
					
					if(count($l_as_data)>0)
					{
						if( !curl_setopt($l_o_ch, CURLOPT_POST, 1) )
							throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_POST)");
						if( !curl_setopt($l_o_ch, CURLOPT_POSTFIELDS, http_build_query($l_as_data)) )
							throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_POSTFIELDS)");
					//	if( !curl_setopt($l_o_ch, CURLOPT_HEADER, 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8') )
						//	throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_setopt(CURLOPT_HEADER_POLUX)");
							
					}
					//return curl_exec($l_o_ch);
				//}
				
				$l_s_result = curl_exec($l_o_ch);
				if(!$l_s_result) 
					throw new JobToolGenericException("Error when init dl parameters : FAIL: curl_exec() => ".curl_error($l_o_ch));
				
				
				curl_close($l_o_ch);
				fclose($l_o_fileOut);

				return $l_s_result;
			}
			else
				throw new JobToolGenericException("Error during outfile opening.");
		}
		else 
			throw new JobToolGenericException("Error during distant filestream init.");
	}
	
	
	// *********************** Private Methods  *********************** //
	/**
	 * cURLcheckBasicFunctions - Check Pre-requisite
	 * 
	 * @uses curl library
	 * @return boolean
	 */	
	private function cURLcheckBasicFunctions()
	{
		if( !function_exists("curl_init") &&
		!function_exists("curl_setopt") &&
		!function_exists("curl_exec") &&
		!function_exists("curl_close") ) return false;
		else return true;
	}

}

?>